package com.icinfo.wanandroid.utils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.support.design.widget.BaseTransientBottomBar;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.icinfo.wanandroid.R;
import com.icinfo.wanandroid.app.WanAndroidApp;

/**
 * Author: 胡高建
 * Comment: //TODO
 * Date: 2019-07-24 10:48
 */
public class CommonUtils {
    public static void showSnackMessage(Activity activity, String msg) {
        LogHelper.e("showSnackMessage : " + msg);
        //去掉虚拟按键
        activity.getWindow().getDecorView().setSystemUiVisibility(
                //隐藏虚拟按键栏|防止点击屏幕时，隐藏的虚拟按键栏又回弹
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE
        );
        final Snackbar snackbar = Snackbar.make(activity.getWindow().getDecorView(), msg, Snackbar.LENGTH_SHORT);
        View view = snackbar.getView();
        ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(ContextCompat.getColor(activity, R.color.white));
        snackbar.setAction("知道了", v -> {
            snackbar.dismiss();
            //隐藏Snackbar时恢复虚拟按键栏，否则屏幕会多出一块空白布局
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }).show();
        snackbar.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
            @Override
            public void onDismissed(Snackbar transientBottomBar, int event) {
                super.onDismissed(transientBottomBar, event);
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        });
    }

    public static boolean isNetworkConnected() {
        ConnectivityManager connectivityManager= (ConnectivityManager) WanAndroidApp.getInstance()
                .getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        assert connectivityManager!=null;
        return connectivityManager.getActiveNetworkInfo()!=null;
    }
}
